package com.hh.releasenode.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hh.releasenode.dto.SysUserDTO;
import com.hh.releasenode.entity.SysUserEntity;
import com.hh.releasenode.form.SysUserForm;
import com.hh.releasenode.form.SysUserStatusForm;
import com.hh.releasenode.form.UpdateSysUserForm;
import com.hh.releasenode.service.SysUserService;
import com.hh.releasenode.utils.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName SysUserController
 * @Description TODO
 * @Author KunxianLi
 * Date 2020/6/12 11:09
 * Version 1.0
 **/
@RestController
@Api(tags = "用户相关接口")
@RequestMapping("/admin")
public class SysUserController {

    @Autowired
    SysUserService sysUserService;

    @ApiOperation("用户注册")
    @PostMapping("/user/register")
    public Response registerUser(@RequestBody @Validated SysUserForm userForm){

        sysUserService.registerUser(userForm);

        return Response.success();

    }

    @ApiOperation("获取用户列表")
    @GetMapping("/users")
    public Response getSysUserPage(
            @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex,
            @RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSize,
            @RequestParam(value = "userName",required = false) String userName,
            @RequestParam(value = "nickName",required = false) String nickName,
            @RequestParam(value = "mobile",required = false) String mobile,
            @RequestParam(value = "status",required = false) Integer status){

        Page<SysUserDTO> page = new Page<>(pageIndex,pageSize);

        Map<String, Object> params = new HashMap<>();
        params.put("userName",userName);
        params.put("nickName",nickName);
        params.put("mobile",mobile);
        params.put("status",status);

        return Response.success(sysUserService.getSysUserPage(page,params));

    }

    @ApiOperation("更新用户状态")
    @PostMapping("/update/status")
    public Response updateSysUserStatus(@RequestBody SysUserStatusForm form){

        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setId(form.getSysUserId());
        sysUserEntity.setStatus(form.getStatus());
        sysUserService.updateById(sysUserEntity);

        return Response.success();

    }

    @ApiOperation("更新用户状态")
    @PostMapping("/update/user")
    public Response updateSysUser(@RequestBody UpdateSysUserForm form){

        sysUserService.updateUserInfo(form);

        return Response.success();

    }




}
